<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   Java中的volatile关键字  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/23596.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466459875: Accept with keywords: (title(0.333333333333):关键字,社区,数盟, topn(0.3):数盟,编译器,时间,内存,赋值,状态,屏障,模式,关键字,指令,文章,排序,数据,语句,变量,对象,语义,原子,计数器,规则,机器指令,关系,顺序,编程语言,中断,方法,代码,程序,线程,处理器).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    Java中的volatile关键字
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    Java中的volatile关键字 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,64] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech/devl">
       编程语言
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/23596.html">
          Java中的volatile关键字
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          491 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech/devl" rel="category tag">
          编程语言
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <div class="content" id="content0">
        <p>
         Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的 synchronized”；与 synchronized 块相比，volatile 变量所需的编码较少， 并且运行时开销也较少，但是它所能实现的功能也仅是 synchronized 的一部分。
        </p>
        <p>
         当volatile用于一个作用域时，Java保证如下：
        </p>
        <p>
         1.（适用于Java所有版本）读和写一个volatile变量有全局的排序。也就是说每个线程访问一个volatile作用域时会在继续执行 之前读取它的当前值， 而不是（可能）使用一个缓存的值。(但是并不保证经常读写volatile作用域时读和写的相对顺序，也就是说通常这并不是有用的线程构建)。
        </p>
        <p>
         2.（适用于Java5及其之后的版本）volatile的读和写建立了一个happens-before关系，类似于申请和释放一个互斥锁。
        </p>
        <p>
         使用volatile会比使用锁更快，但是在一些情况下它不能工作。volatile使用范围在Java5中得到了扩展，特别是双重检查锁定现 在能够正确工作。 volatile可以用在任何变量前面，但不能用于final变量前面，因为final型的变量是禁止修改的。也不存在线程安全的问题。
        </p>
        <p>
         了解volatile关键字关键字之前，首先了解下原子操作和happens-before关系的相关概念。
        </p>
        <h3>
         原子操作
        </h3>
        <p>
         原子（atom）本意是“不能被进一步分割的最小粒子”，而原子操作（atomic operation）意为”不可被中断的一个或一系列操作” 。 原子操作在操作完毕之前不会线程调度器中断。在Java中，对除了long和double之外的基本类型的简单操作都具有原子性。 简单操作就是赋值或者return。比如”a = 1;“和 “return a;”这样的操作都具有原子性。但是在Java中，类似“a += b”这样的操作不具有原子性， 所以如果方法不是同步的就会出现难以预料的结果。在某些JVM中”a += b”可能要经过这样三个步骤： 1.取出a和b。2.计算a+b。3.将计算结果写入内存。
        </p>
        <p>
         如果有两个线程t1,t2在进行这样的操作。t1在第二步做完之后还没来得及把数据写回内存就被线程调度器中断了，于是t2开始执行， t2执行完毕后t1又把没有完成的第三步做完。这个时候就出现了错误，相当于t2的计算结果被无视掉了。 类似的，像“a++”这样的操作也都不具有原子性。所以在多线程的环境下一定要记得进行同步操作。
        </p>
        <p>
         要搞清楚这个问题，首先应该明白计算机内部都做什么了。比如做了一个i++操作，计算机内部做了三次处理：读取－修改－写入。 同样，对于一个long型数据，做了个赋值操作，在32系统下需要经过两步才能完成，先修改低32位，然后修改高32位。
        </p>
        <p>
         假想一下，当将以上的操作放到一个多线程环境下操作时候，有可能出现的问题，是这些步骤执行了一部分，而另外一个线程就已经引用了变量值， 这样就导致了读取脏数据的问题。
        </p>
        <h3>
         happens-before关系
        </h3>
        <p>
         Java语言中有一个“先行发生”（happen—before）的规则，它是Java内存模型中定义的两项操作之间的偏序关系，如果操作A先 行发生于操作B， 其意思就是说，在发生操作B之前，操作A产生的影响都能被操作B观察到，“影响”包括修改了内存中共享变量的值、发送了消息、调用了方法等， 它与时间上的先后发生基本没有太大关系。这个原则特别重要，它是判断数据是否存在竞争、线程是否安全的主要依据。
        </p>
        <p>
         举例来说，假设存在如下三个线程，分别执行对应的操作:
        </p>
        <p>
         线程A中执行如下操作：i=1。
        </p>
        <p>
         线程B中执行如下操作：j=i。
        </p>
        <p>
         线程C中执行如下操作：i=2。
        </p>
        <p>
         假设线程A中的操作”i=1“ happen—before线程B中的操作“j=i”，那么就可以保证在线程B的操作执行后，变量j的值一定为1， 即线程B观察到了线程A中操作“i=1”所产生的影响；现在，我们依然保持线程A和线程B之间的happen—before关系，同时线程C出现在 了线程A和线程B的操作之间，但是C与B并没有happen—before关系，那么j的值就不确定了，线程C对变量i的影响可能会被线程B观察到， 也可能不会，这时线程B就存在读取到不是最新数据的风险，不具备线程安全性。
        </p>
        <p>
         下面是Java内存模型中的八条可保证happen—before的规则，它们无需任何同步器协助就已经存在，可以在编码中直接使用。 如果两个操作之间的关系不在此列，并且无法从下列规则推导出来的话，它们就没有顺序性保障，虚拟机可以对它们进行随机地重排序。
        </p>
        <p>
         1、程序次序规则：在一个单独的线程中，按照程序代码的执行流顺序，（时间上）先执行的操作happen—before（时间上）后执行的操作。
        </p>
        <p>
         2、管理锁定规则：一个unlock操作happen—before后面（时间上的先后顺序，下同）对同一个锁的lock操作。
        </p>
        <p>
         3、volatile变量规则：对一个volatile变量的写操作happen—before后面对该变量的读操作。
        </p>
        <p>
         4、线程启动规则：Thread对象的start（）方法happen—before此线程的每一个动作。
        </p>
        <p>
         5、线程终止规则：线程的所有操作都happen—before对此线程的终止检测，可以通过Thread.join（）方法结束、Thread.isAlive（） 的返回值等手段检测到线程已经终止执行。
        </p>
        <p>
         6、线程中断规则：对线程interrupt（）方法的调用happen—before发生于被中断线程的代码检测到中断时事件的发生。
        </p>
        <p>
         7、对象终结规则：一个对象的初始化完成（构造函数执行结束）happen—before它的finalize（）方法的开始。
        </p>
        <p>
         8、传递性：如果操作A happen—before操作B，操作B happen—before操作C，那么可以得出A happen—before操作C。
        </p>
        <h4>
         时间上先后顺序和happen—before原则
        </h4>
        <p>
         “时间上执行的先后顺序”与“happen—before”之间有何不同呢？
        </p>
        <p>
         1、首先来看操作A在时间上先与操作B发生，是否意味着操作A happen—before操作B？
        </p>
        <p>
         一个常用来分析的例子如下：
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e01976d339036896" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           &lt;span class="token keyword"&gt;private&lt;/span&gt; &lt;span class="token keyword"&gt;int&lt;/span&gt; value &lt;span class="token operator"&gt;=&lt;/span&gt; &lt;span class="token number"&gt;0&lt;/span&gt;&lt;span class="token punctuation"&gt;;&lt;/span&gt;
    &lt;span class="token keyword"&gt;public&lt;/span&gt; &lt;span class="token keyword"&gt;int&lt;/span&gt; &lt;span class="token function"&gt;get&lt;/span&gt;&lt;span class="token punctuation"&gt;(&lt;/span&gt;&lt;span class="token punctuation"&gt;)&lt;/span&gt;&lt;span class="token punctuation"&gt;{&lt;/span&gt;
        &lt;span class="token keyword"&gt;return&lt;/span&gt; value&lt;span class="token punctuation"&gt;;&lt;/span&gt;
    &lt;span class="token punctuation"&gt;}&lt;/span&gt;
    &lt;span class="token keyword"&gt;public&lt;/span&gt; &lt;span class="token keyword"&gt;void&lt;/span&gt; &lt;span class="token function"&gt;set&lt;/span&gt;&lt;span class="token punctuation"&gt;(&lt;/span&gt;&lt;span class="token keyword"&gt;int&lt;/span&gt; value&lt;span class="token punctuation"&gt;)&lt;/span&gt;&lt;span class="token punctuation"&gt;{&lt;/span&gt;
        &lt;span class="token keyword"&gt;this&lt;/span&gt;&lt;span class="token punctuation"&gt;.&lt;/span&gt;value &lt;span class="token operator"&gt;=&lt;/span&gt; value&lt;span class="token punctuation"&gt;;&lt;/span&gt;
    &lt;span class="token punctuation"&gt;}&lt;/span&gt;
&lt;span class="token punctuation"&gt;}&lt;/span&gt;
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e01976d339036896-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01976d339036896-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e01976d339036896-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01976d339036896-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e01976d339036896-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01976d339036896-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e01976d339036896-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01976d339036896-8">
                8
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e01976d339036896-1">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token operator"
                </span>
                <span class="crayon-o">
                 &gt;=
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token number"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01976d339036896-2">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token function"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-v">
                 get
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01976d339036896-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01976d339036896-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 }
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01976d339036896-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token function"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-v">
                 set
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01976d339036896-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token keyword"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-r">
                 this
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token operator"
                </span>
                <span class="crayon-o">
                 &gt;=
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01976d339036896-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 }
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01976d339036896-8">
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-e">
                 span
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-s">
                 "token punctuation"
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
                <span class="crayon-sy">
                 }
                </span>
                <span class="crayon-o">
                 &lt;
                </span>
                <span class="crayon-o">
                 /
                </span>
                <span class="crayon-v">
                 span
                </span>
                <span class="crayon-o">
                 &gt;
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0637 seconds] -->
        <p>
         假设存在线程A和线程B，线程A先（时间上的先）调用了setValue（3）操作，然后（时间上的后）线程B调用了同一对象的 getValue（）方法， 那么线程B得到的返回值一定是3吗？ 对照以上八条happen—before规则，发现没有一条规则适合于这里的value变量，从而我们可以判定线程A中的setValue（3）操作与 线程B中的getValue（）操作不存在happen—before关系。因此，尽管线程A的setValue（3）在操作时间上先于操作B的 getvalue（）， 但无法保证线程B的getValue（）操作一定观察到了线程A的setValue（3）操作所产生的结果，也即是getValue（）的返回值不一定为 3 （有可能是之前setValue所设置的值）。这里的操作不是线程安全的。
        </p>
        <p>
         因此，“一个操作时间上先发生于另一个操作”并不代表“一个操作happen—before另一个操作”。
        </p>
        <p>
         解决方法：可以将setValue（int）方法和getValue（）方法均定义为synchronized方法，也可以把value定义为 volatile变量 （value的修改并不依赖value的原值，符合volatile的使用场景），分别对应happen—before规则的第2和第3条。注意， 只将setValue（int）方法和getvalue（）方法中的一个定义为synchronized方法是不行的，必须对同一个变量的所有读写同步， 才能保证不读取到陈旧的数据，仅仅同步读或写是不够的 。
        </p>
        <p>
         2、其次来看，操作A happen—before操作B，是否意味着操作A在时间上先与操作B发生？
        </p>
        <p>
         如果A happens- before B，JMM并不要求A一定要在B之前执行。JMM仅仅要求前一个操作（执行的结果）对后一个操作可见， 且前一个操作按顺序排在第二个操作之前。CPU是可以不按我们写代码的顺序执行内存的存取过程的，也就是指令会乱序或并行运行， 只有上面的happens-before所规定的情况下，才保证顺序性，确保任何内存的写，对其他语句都是可见的。
        </p>
        <p>
         假设同一个线程执行上面两个操作：操作A：x=1和操作B：y=2。根据happen—before规则的第1条，操作A happen—before 操作B， 但是由于编译器的指令重排序（Java语言规范规定了JVM线程内部维持顺序化语义，也就是说只要程序的最终结果等同于它在严格的顺序化环境下的结果， 那么指令的执行顺序就可能与代码的顺序不一致。这个过程通过叫做指令的重排序。指令重排序存在的意义在于：JVM能够根据处理器的特性 （CPU的多级缓存系统、多核处理器等）适当的重新排序机器指令，使机器指令更符合CPU的执行特点，最大限度的发挥机器的性能。 在没有同步的情况下，编译器、处理器以及运行时等都可能对操作的执行顺序进行一些意想不到的调整）等原因，操作A在时间上有可能后于操作B被 处理器执行，但这并不影响happen—before原则的正确性。因此，“一个操作happen—before另一个操作”并不代表“一个操作时间上先 发生于另一个操作”。
        </p>
        <p>
         最后，一个操作和另一个操作必定存在某个顺序，要么一个操作或者是先于或者是后于另一个操作，或者与两个操作同时发生。 同时发生是完全可能存在的，特别是在多CPU的情况下。而两个操作之间却可能没有happen-before关系，也就是说有可能发生这样的情况， 操作A不happen-before操作B，操作B也不happen-before操作A，用数学上的术语happen-before关系是个偏序关系。 两个存在happen-before关系的操作不可能同时发生，一个操作A happen-before操作B，它们必定在时间上是完全错开的， 这实际上也是同步的语义之一（独占访问）。
        </p>
        <p>
         举例说明如下：
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e019782322196240" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           public class Test {
    private int a = 0;
    private long b = 0;
    public void set() {
        a = 1;
        b = -1;
    }
    public void check() {
        if (! ((b == 0) || (b == -1 &amp;amp;&amp;amp; a == 1))
            throw new Exception("check Error!");
    }
}
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e019782322196240-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-576866e019782322196240-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019782322196240-14">
                14
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e019782322196240-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-2">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Test
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 a
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 long
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 b
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 set
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 a
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 b
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 -
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 check
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 if
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 !
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 b
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 ==
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 0
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 ||
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 b
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 ==
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 -
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 amp
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-o">
                 &amp;
                </span>
                <span class="crayon-v">
                 amp
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 a
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 ==
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-cn">
                 1
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 throw
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 new
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Exception
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 "check Error!"
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019782322196240-13">
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019782322196240-14">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0294 seconds] -->
        <p>
         对于set()方法的执行： 1. 编译器可以重新安排语句的执行顺序，这样b就可以在a之前赋值。如果方法是内嵌的(inline)，编译器还可以把其它语句重新排序。 2. 处理器可以改变这些语句的机器指令的执行顺序，甚到同时执行这些语句。 3. 存储系统(由于被缓存控制单元控制)也可以重新安排对应存储单元的写操作顺序，这些写操作可能与其他计算和存储操作同时发生。 4. 编译器，处理器和存储系统都可以把这两条语句的机器指令交叉执行。 例如：在一台32位的机器上，可以先写b的高位，然后写a，最后写b的低位，(注：b为long类型，在32位的机器上分高低位存储) 5. 编译器，处理器和存储系统都可以使对应于变量的存储单元一直保留着原来的值， 以某种方式维护相应的值(例如，在CPU的寄存器中) 以保证代码正常运行，直到下一个check调用才更新。
        </p>
        <p>
         在单线程(或同步)的情况下，上面的check()永远不会报错， 但非同步多线程运行时却很有可能。 并且，多个CPU之间的缓存也不保证实时同步，也就是说你刚给一个变量赋值，另一个线程立即获取它的值，可能拿到的却是旧值(或null)， 因为两个线程在不同的CPU执行，它们看到的缓存值不一样，只有在synchronized或volatile或final的性况下才能保证正确性。
        </p>
        <p>
         例如：
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e01978a729996168" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           public class Test {
        private int n;
        public void set(int n) {
            this.n = n;
        }
        public void check() {
            if (n != n)
                throw new Exception("check Error!");
        }
    }
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e01978a729996168-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e01978a729996168-12">
                12
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e01978a729996168-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-2">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Test
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01978a729996168-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 set
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01978a729996168-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 this
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01978a729996168-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 check
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 if
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 !=
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 n
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01978a729996168-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 throw
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 new
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Exception
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 "check Error!"
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e01978a729996168-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e01978a729996168-12">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0120 seconds] -->
        <p>
         check()中的 n != n 好像永远不会成立，因为他们指向同一个值，但非同步时却很有可能发生。
        </p>
        <h3>
         volatile使用情形
        </h3>
        <p>
         只能在有限的一些情形下使用 volatile 变量替代锁。要使 volatile 变量提供理想的线程安全，必须同时满足下面两个条件：
        </p>
        <p>
         1.对变量的写操作不依赖于当前值。
        </p>
        <p>
         2.该变量没有包含在具有其他变量的不变式中。
        </p>
        <h4>
         模式1：状态标志
        </h4>
        <p>
         也许实现 volatile 变量的规范使用仅仅是使用一个布尔状态标志，用于指示发生了一个重要的一次性事件，例如完成初始化或请求停机。 很多应用程序包含了一种控制结构，形式为 “在还没有准备好停止程序时再执行一些工作”，如下所示：
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e019792967626679" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           volatile boolean shutdownRequested;
...
public void shutdown() { shutdownRequested = true; }
public void doWork() {
    while (!shutdownRequested) {
        // do stuff
    }
}
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e019792967626679-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019792967626679-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e019792967626679-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019792967626679-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e019792967626679-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019792967626679-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e019792967626679-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019792967626679-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e019792967626679-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019792967626679-10">
                10
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e019792967626679-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019792967626679-2">
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 boolean
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 shutdownRequested
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019792967626679-3">
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-sy">
                 .
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019792967626679-4">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 shutdown
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 shutdownRequested
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 true
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019792967626679-5">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 doWork
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019792967626679-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 while
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-o">
                 !
                </span>
                <span class="crayon-v">
                 shutdownRequested
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019792967626679-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // do stuff
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019792967626679-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019792967626679-9">
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019792967626679-10">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0182 seconds] -->
        <p>
         很可能会从循环外部调用 shutdown() 方法，即在另一个线程中。因此，需要执行某种同步来确保正确实现 shutdownRequested 变量的可见性。 然而，使用 synchronized 块编写循环要比使用如上所示的 volatile 状态标志编写麻烦很多。由于 volatile 简化了编码，并且状态标志并不依赖于程序内任何其他状态，因此此处非常适合使用 volatile。
        </p>
        <h4>
         模式2：一次性安全发布（one-time safe publication）
        </h4>
        <p>
         缺乏同步会导致无法实现可见性，这使得确定何时写入对象引用而不是原语值变得更加困难。在缺乏同步的情况下，可能会遇到某个对象引用的更新值 （由另一个线程写入）和该对象状态的旧值同时存在。（这就是造成著名的双重检查锁定（double-checked-locking）问题的根源， 其中对象引用在没有同步的情况下进行读操作，产生的问题是您可能会看到一个更新的引用，但是仍然会通过该引用看到不完全构造的对象）。 实现安全发布对象的一种技术就是将对象引用定义为 volatile 类型。下面的代码展示了一个示例，其中后台线程在启动阶段从数据库加载一些数据。 其他代码在能够利用这些数据时，在使用之前将检查这些数据是否曾经发布过。
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e019799592428950" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           public class BackgroundFloobleLoader {
    public volatile Flooble theFlooble;
    public void initInBackground() {
        // do lots of stuff
        theFlooble = new Flooble();  // this is the only write to theFlooble
    }
public class SomeOtherClass {
    public void doWork() {
        while (true) {
            // do some stuff...
            // use the Flooble, but only if it is ready
            if (floobleLoader.theFlooble != null)
                doSomething(floobleLoader.theFlooble);
        }
    }
}
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e019799592428950-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-576866e019799592428950-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e019799592428950-18">
                18
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e019799592428950-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-2">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 BackgroundFloobleLoader
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-e">
                 Flooble
                </span>
                <span class="crayon-v">
                 theFlooble
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 initInBackground
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // do lots of stuff
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 theFlooble
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 new
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Flooble
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // this is the only write to theFlooble
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-8">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 SomeOtherClass
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 doWork
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 while
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 true
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // do some stuff...
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // use the Flooble, but only if it is ready
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-13">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 if
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 floobleLoader
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 theFlooble
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 !=
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 null
                </span>
                <span class="crayon-sy">
                 )
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-14">
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 doSomething
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 floobleLoader
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 theFlooble
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-15">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-16">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e019799592428950-17">
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e019799592428950-18">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0278 seconds] -->
        <p>
         如果 theFlooble 引用不是 volatile 类型，doWork() 中的代码在解除对 theFlooble 的引用时，将会得到一个不完全构造的 Flooble。 该模式的一个必要条件是：被发布的对象必须是线程安全的，或者是有效的不可变对象（有效不可变意味着对象的状态在发布之后永远不会被修改）。 volatile 类型的引用可以确保对象的发布形式的可见性，但是如果对象的状态在发布后将发生更改，那么就需要额外的同步。
        </p>
        <h4>
         模式3：独立观察（independent observation）
        </h4>
        <p>
         安全使用 volatile 的另一种简单模式是：定期 “发布” 观察结果供程序内部使用。例如，假设有一种环境传感器能够感觉环境温度。 一个后台线程可能会每隔几秒读取一次该传感器，并更新包含当前文档的 volatile 变量。然后，其他线程可以读取这个变量， 从而随时能够看到最新的温度值。 使用该模式的另一种应用程序就是收集程序的统计信息。下面展示了身份验证机制如何记忆最近一次登录的用户的名字。将反复使用 lastUser 引用来发布值，以供程序的其他部分使用。
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e0197a1480544476" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           public class UserManager {
        public volatile String lastUser;
        public boolean authenticate(String user, String password) {
            boolean valid = passwordIsValid(user, password);
            if (valid) {
                User u = new User();
                activeUsers.add(u);
                lastUser = user;
            }
            return valid;
        }
    }
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a1480544476-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a1480544476-14">
                14
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e0197a1480544476-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-2">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 UserManager
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-3">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastUser
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 boolean
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 authenticate
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 user
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 password
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 boolean
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 valid
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 passwordIsValid
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 user
                </span>
                <span class="crayon-sy">
                 ,
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 password
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 if
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 valid
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-i">
                 User
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 u
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 new
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 User
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 activeUsers
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-e">
                 add
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-v">
                 u
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastUser
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 user
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 valid
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a1480544476-13">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a1480544476-14">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0149 seconds] -->
        <p>
         该模式是前面模式的扩展；将某个值发布以在程序内的其他地方使用，但是与一次性事件的发布不同， 这是一系列独立事件。这个模式要求被发布的值是有效不可变的 —— 即值的状态在发布后不会更改。使用该值的代码需要清楚该值可能随时发生变化。
        </p>
        <h4>
         模式4：“volatile bean” 模式
        </h4>
        <p>
         volatile bean 模式适用于将 JavaBeans 作为“荣誉结构”使用的框架。在 volatile bean 模式中，JavaBean 被用作一组具有 getter 和/或 setter 方法 的独立属性的容器。volatile bean 模式的基本原理是：很多框架为易变数据的持有者（例如 HttpSession） 提供了容器，但是放入这些容器中的对象必须是线程安全的。
        </p>
        <p>
         在 volatile bean 模式中，JavaBean 的所有数据成员都是 volatile 类型的，并且 getter 和 setter 方法必须非常普通， 除了获取或设置相应的属性外，不能包含任何逻辑。此外，对于对象引用的数据成员，引用的对象必须是有效不可变的。 （这将禁止具有数组值的属性，因为当数组引用被声明为 volatile 时，只有引用而不是数组本身具有 volatile 语义）。 对于任何 volatile 变量，不变式或约束都不能包含 JavaBean 属性。下面示例展示了遵守 volatile bean 模式的 JavaBean：
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e0197a8247556799" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           @ThreadSafe
public class Person {
    private volatile String firstName;
    private volatile String lastName;
    private volatile int age;
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public int getAge() { return age; }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-12">
                12
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-13">
                13
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-14">
                14
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-15">
                15
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-16">
                16
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-17">
                17
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-18">
                18
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197a8247556799-19">
                19
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197a8247556799-20">
                20
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e0197a8247556799-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-2">
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 ThreadSafe
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-3">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 Person
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 firstName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 age
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 getFirstName
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 firstName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 getLastName
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 getAge
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 age
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 setFirstName
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 firstName
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-11">
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 this
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 firstName
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 firstName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-12">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-13">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 setLastName
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 String
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastName
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-14">
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 this
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 lastName
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 lastName
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-15">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-16">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 void
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 setAge
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 age
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-17">
                <span class="crayon-h">
                </span>
                <span class="crayon-r">
                 this
                </span>
                <span class="crayon-sy">
                 .
                </span>
                <span class="crayon-v">
                 age
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-o">
                 =
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 age
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-18">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197a8247556799-19">
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197a8247556799-20">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0259 seconds] -->
        <p>
        </p>
        <h4>
         模式5：开销较低的读－写锁策略
        </h4>
        <p>
         目前为止，您应该了解了 volatile 的功能还不足以实现计数器。因为 ++x 实际上是三种操作（读、添加、存储）的简单组合， 如果多个线程凑巧试图同时对 volatile 计数器执行增量操作，那么它的更新值有可能会丢失。 然而，如果读操作远远超过写操作，您可以结合使用内部锁和 volatile 变量来减少公共代码路径的开销。下面的代码显示的线程安全的计数器使用 synchronized 确保增量操作是原子的，并使用 volatile 保证当前结果的可见性。如果更新不频繁的话，该方法可实现更好的性能， 因为读路径的开销仅仅涉及 volatile 读操作，这通常要优于一个无竞争的锁获取的开销。
        </p>
        <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
        <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-576866e0197b0028894337" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
         <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <span class="crayon-title">
          </span>
          <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-wrap-button" title="切换自动换行">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-expand-button" title="点击展开代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-copy-button" title="复制代码">
            <div class="crayon-button-icon">
            </div>
           </div>
           <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
            <div class="crayon-button-icon">
            </div>
           </div>
          </div>
         </div>
         <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
         </div>
         <div class="crayon-plain-wrap">
          <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
           @ThreadSafe
public class CheesyCounter {
    // Employs the cheap read-write lock trick
    // All mutative operations MUST be done with the 'this' lock held
    @GuardedBy("this") private volatile int value;
    public int getValue() { return value; }
    public synchronized int increment() {
        return value++;
    }
}
          </textarea>
         </div>
         <div class="crayon-main" style="">
          <table class="crayon-table">
           <tbody>
            <tr class="crayon-row">
             <td class="crayon-nums " data-settings="show">
              <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-1">
                1
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-2">
                2
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-3">
                3
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-4">
                4
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-5">
                5
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-6">
                6
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-7">
                7
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-8">
                8
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-9">
                9
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-10">
                10
               </div>
               <div class="crayon-num" data-line="crayon-576866e0197b0028894337-11">
                11
               </div>
               <div class="crayon-num crayon-striped-num" data-line="crayon-576866e0197b0028894337-12">
                12
               </div>
              </div>
             </td>
             <td class="crayon-code">
              <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
               <div class="crayon-line" id="crayon-576866e0197b0028894337-1">
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-2">
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 ThreadSafe
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197b0028894337-3">
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 class
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 CheesyCounter
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-4">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // Employs the cheap read-write lock trick
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197b0028894337-5">
                <span class="crayon-h">
                </span>
                <span class="crayon-c">
                 // All mutative operations MUST be done with the 'this' lock held
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-6">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 @
                </span>
                <span class="crayon-e">
                 GuardedBy
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-s">
                 "this"
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 private
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 volatile
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197b0028894337-7">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 getValue
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-sy">
                 ;
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-8">
                <span class="crayon-h">
                </span>
                <span class="crayon-m">
                 public
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 synchronized
                </span>
                <span class="crayon-t">
                 int
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-e">
                 increment
                </span>
                <span class="crayon-sy">
                 (
                </span>
                <span class="crayon-sy">
                 )
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 {
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197b0028894337-9">
                <span class="crayon-h">
                </span>
                <span class="crayon-st">
                 return
                </span>
                <span class="crayon-h">
                </span>
                <span class="crayon-v">
                 value
                </span>
                <span class="crayon-o">
                 ++
                </span>
                <span class="crayon-sy">
                 ;
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-10">
                <span class="crayon-h">
                </span>
                <span class="crayon-sy">
                 }
                </span>
               </div>
               <div class="crayon-line" id="crayon-576866e0197b0028894337-11">
                <span class="crayon-sy">
                 }
                </span>
                <span class="crayon-h">
                </span>
               </div>
               <div class="crayon-line crayon-striped-line" id="crayon-576866e0197b0028894337-12">
                <span class="crayon-h">
                </span>
               </div>
              </div>
             </td>
            </tr>
           </tbody>
          </table>
         </div>
        </div>
        <!-- [Format Time: 0.0088 seconds] -->
        <p>
         之所以将这种技术称之为 “开销较低的读－写锁” 是因为您使用了不同的同步机制进行读写操作。因为本例中的写操作违反了使用 volatile 的第一个条件，因此不能使用 volatile 安全地实现计数器 —— 您必须使用锁。然而，您可以在读操作中使用 volatile 确保当前值的可见性，因此可以使用锁进行所有变化的操作，使用 volatile 进行只读操作。其中，锁一次只允许一个线程访问值， volatile 允许多个线程执行读操作，因此当使用 volatile 保证读代码路径时，要比使用锁执行全部代码路径获得更高的共享度， 就像读－写操作一样。然而，要随时牢记这种模式的弱点：如果超越了该模式的最基本应用，结合这两个竞争的同步机制将变得非常困难。
        </p>
        <h3>
         volatile的原理和实现机制
        </h3>
        <p>
         观察加入volatile关键字和没有加入volatile关键字时所生成的汇编代码发现，加入volatile关键字时，会多出一个lock前缀指令, lock前缀指令实际上相当于一个内存屏障（也成内存栅栏），内存屏障会提供3个功能：
        </p>
        <p>
         1）它确保指令重排序时不会把其后面的指令排到内存屏障之前的位置，也不会把前面的指令排到内存屏障的后面； 即在执行到内存屏障这句指令时，在它前面的操作已经全部完成；
        </p>
        <p>
         2）它会强制将对缓存的修改操作立即写入主存；
        </p>
        <p>
         3）如果是写操作，它会导致其他CPU中对应的缓存行无效。
        </p>
        <p>
         变量在有了volatile修饰之后，对变量的修改会有一个内存屏障的保护，使得后面的指令不能被重排序到内存屏障之前的位置。 volalite变量的读性能与普通变量类似，但是写性能要低一些，因为它需要插入内存屏障指令来保证处理器不会发生乱序执行。 即便如此，大多数场景下volatile的总开销仍然要比锁低，所以volatile的语义能满足需求时候，选择volatile要优于使用锁。
        </p>
       </div>
       <div>
        <div class="adsense">
        </div>
        <div class="post-meta">
         文章出处：
         <a class="writer-profile-permalink" href="http://souly.cn/%E6%8A%80%E6%9C%AF%E5%8D%9A%E6%96%87/2016/04/11/Java%E4%B8%AD%E7%9A%84volatile%E5%85%B3%E9%94%AE%E5%AD%97/" rel="nofollow" target="_blank">
          SOULY
         </a>
         。This entry was posted in
         <a href="http://netsmell.com/cat/programming" rel="category tag">
          Programming
         </a>
         and tagged
         <a href="http://netsmell.com/tag/java" rel="tag">
          java
         </a>
         ,
         <a href="http://netsmell.com/tag/volatile" rel="tag">
          volatile
         </a>
         . Bookmark the
         <a href="http://netsmell.com/post/java-volatile-keyword.html" rel="bookmark" title="Permalink to Java中的volatile关键字">
          permalink
         </a>
         .
        </div>
       </div>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/24678.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/20140917125452915416-216x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24678.html" rel="bookmark" title="python3中的正则模块">
         python3中的正则模块
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24675.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/t015b337bd75d9ef893-161x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24675.html" rel="bookmark" title="注释是恶魔，请不要再写一行注释">
         注释是恶魔，请不要再写一行注释
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24660.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/u16130037972892789947fm21gp0-300x157.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24660.html" rel="bookmark" title="如何从Github上轻松安装R包">
         如何从Github上轻松安装R包
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24654.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/df53fac99fc53ba5a90666abcca25e6d_b-267x200.png"/>
        </a>
        <a class="link" href="http://dataunion.org/24654.html" rel="bookmark" title="简单形象又有趣地说说强大的神经网络">
         简单形象又有趣地说说强大的神经网络
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F23596.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/23596.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/23596.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>